package sort

import (
	"algorithm-golang/lib"
	"log"
	"time"
)

type SortFunc func([]int)
type SortBoundFunc func([]int, int, int)

func TimeSpentBound(fn SortBoundFunc, data []int, l, r int) {
	start := time.Now()
	fn(data, l, r)
	log.Printf("n = %d, cost time: %vs\n",
		len(data), time.Since(start).Seconds())
}

func TimeSpent(fn SortFunc, data []int) {
	start := time.Now()
	fn(data)
	log.Printf("n = %d, cost time: %vs\n",
		len(data), time.Since(start).Seconds())
}

func Test(t Type, data []int) {
	start := time.Now()
	switch t {
	case Selection:
		SelectionSort(data)
	case Insertion:
		InsertionSort(data)
	case Heap:
		HeapSort(data)
	default:
		log.Printf("unknown sort type: %d\n", t)
	}
	log.Printf("n = %d, cost time: %vs\n", len(data), time.Since(start).Seconds())
	if !lib.IsSorted(data) {
		log.Println("排序失败")
		return
	}
}
